{***************************************************************
    SpyGestion  Copyright (C) 2008 Christophe KOALI <nature2gas@gmail.com>
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
****************************************************************}
unit modParsingClassIFlotte3;

interface

uses modParsingClassBase,
     modBaseType,
     Classes,
     sFonctionsChaines,
     SysUtils,
     Windows,

     LibXmlParser;

const
    CNT_FLOTTE3_IN_METAL        = '<<metal>>';
    CNT_FLOTTE3_IN_CRISTAL      = '<<cristal>>';
    CNT_FLOTTE3_IN_DEUTERIUM    = '<<deuterium>>';
    CNT_FLOTTE3_IN_ORDRE        = '<<ordre>>';


type
    ////////////////////////////////////////////////////////////////////
    // Page flotte 3, sélection des ressources et de l'ordre
    TParsingFlotte3_OrdreEtRessources = class(IParsingRedirection)
    private
       //
       // Informations qui vont être extraites
       //

        FDataToPost: string;
        FPageToPost: string;
        
        FListOrdreDispo: TStringList;

        // Ressources actuelles
        FCurrentRessource: TOGRessourceAll;


       //
       // Variables qui vont aider à se repérer dans la page
       //

        FInForm: boolean;
        FRadioDejaTraiter: boolean;


    public
       property DataToPost: string read FDataToPost;
       property PageToPost : string read FPageToPost;

       function ExistThisOrdre(Ordre: string): boolean;
       function ExistOrdre: boolean;


       property CurrRessource: TOGRessourceAll read FCurrentRessource write FCurrentRessource;



       /////////////////////////////////////////////////////////////////////////
       // Implémentation de IParsingRedirection

        // initialise l'interface
        procedure Initialisation; override;

        // obtient le résultat d'analyse
        function  GetFinalState: TParsingResultState; override;

        procedure SetFullURLFromBase(const BaseLink: string); override;


        procedure ScanOnStartTag(Sender: TObject; TagName: string; Attributes: TAttrList); override;
        procedure ScanOnEndTag(Sender: TObject; TagName: string); override;
        procedure ScanOnContent(Sender: TObject; Content: string); override;

       //
       /////////////////////////////////////////////////////////////////////////

       constructor Create; 
       destructor Destroy; override;

    end;


implementation

uses modLanguageParsing,
     sHTMLFonctions,
     modApercuMsg,
     ESBDates,
     modCore;


procedure TParsingFlotte3_OrdreEtRessources.Initialisation;
begin

    inherited;

       //
       // Informations qui vont être extraites
       //

    FDataToPost := '';
    FPageToPost := '';

    // Ressources actuelles
    FCurrentRessource.Metal := -1;
    FCurrentRessource.Cristal := -1;
    FCurrentRessource.Deuterium := -1;
    FCurrentRessource.Energie := 0;

    FListOrdreDispo.Clear;



       //
       // Variables qui vont aider à se repérer dans la page
       //

    FInForm := false;
    FRadioDejaTraiter := false;

    
end;

function TParsingFlotte3_OrdreEtRessources.GetFinalState: TParsingResultState;
begin
    Result := PRESULT_ECHEC;
    
    if (FCurrentRessource.Metal < 0) or (FCurrentRessource.Cristal < 0) or (FCurrentRessource.Deuterium < 0) then
       FError := true;

    if FError then begin
       exit;
    end;



    Result := NoT(PRESULT_ECHEC);
end;

constructor TParsingFlotte3_OrdreEtRessources.Create;
begin
    inherited;

    FListOrdreDispo := TStringList.Create;
end;

destructor TParsingFlotte3_OrdreEtRessources.Destroy;
begin
    FListOrdreDispo.Free;

    inherited;
end;

function TParsingFlotte3_OrdreEtRessources.ExistOrdre: boolean;
begin
   Result := FListOrdreDispo.Count > 0;
end;

function TParsingFlotte3_OrdreEtRessources.ExistThisOrdre(Ordre: string): boolean;
var i: integer;
begin

   // L'ordre est un nombre inférieur à 10 et supérieur à 0
   i := FListOrdreDispo.IndexOf(Ordre);

   if i >= 0 then
      Result := true
   else
      Result := false;
      

end;

procedure TParsingFlotte3_OrdreEtRessources.SetFullURLFromBase(const BaseLink: string);
begin
    FPageToPost  := sHTML_Get_URL_Lien(BaseLink, FPageToPost);

    inherited;
end;

procedure TParsingFlotte3_OrdreEtRessources.ScanOnStartTag(Sender: TObject; TagName: string; Attributes: TAttrList);
var CurrBalise: string;
begin

    TagName := lowercase(TagName);

    if TagName = 'form' then begin
       // <form action="flottenversand.php?session=0000000" method="POST">

       CurrBalise := Attributes.Value('action');

       FPageToPost := CurrBalise;
       FInForm := true;

    end else if FInForm and (TagName = 'input') then begin
        // <input name="thisgalaxy" type="hidden" value="4" />

        CurrBalise := trim(Attributes.Value('type'));

        if SameText(CurrBalise, 'hidden') then begin
           CurrBalise := Attributes.Value('name') + '=' + Attributes.Value('value');

           if FDataToPost = '' then
              FDataToPost := CurrBalise
           else
              FDataToPost := FDataToPost + '&' + CurrBalise;


           CurrBalise := Attributes.Value('name');


           if SameText(CurrBalise, 'thisresource1') then begin
              // Metal
              FCurrentRessource.Metal := StrToIntDef(Attributes.Value('value'), -1);

           end else if SameText(CurrBalise, 'thisresource2') then begin
              // Cristal
              FCurrentRessource.Cristal := StrToIntDef(Attributes.Value('value'), -1);

           end else if SameText(CurrBalise, 'thisresource3') then begin
              // Deuterium
              FCurrentRessource.Deuterium := StrToIntDef(Attributes.Value('value'), -1);

           end;



        end else if (CurrBalise = '') or (SameText(CurrBalise, 'text')) then begin
           // Simple inputbox
           // On saisie normalement les ressources désirées
           // Il y a que trois inputbox de ce type:
           // <input name="resource1" type="text" alt="Métal " size="10" onChange="calculateTransportCapacity();" />
           // <input name="resource2" type="text" alt="Cristal " size="10" onChange="calculateTransportCapacity();" />
           // <input name="resource3" type="text" alt="Deuterium " size="10" onChange="calculateTransportCapacity();" />

           if FDataToPost = '' then begin
              // Il y a obligatoirement déjà quelque chose dans FDataToPost
              FError := true;
              exit;
           end;

           CurrBalise := Attributes.Value('name');

           if SameText(CurrBalise, 'resource1') then begin
              FDataToPost := FDataToPost + '&' + CurrBalise + '=' + CNT_FLOTTE3_IN_METAL;

           end else if SameText(CurrBalise, 'resource2') then begin
              FDataToPost := FDataToPost + '&' + CurrBalise + '=' + CNT_FLOTTE3_IN_CRISTAL;

           end else if SameText(CurrBalise, 'resource3') then begin
              FDataToPost := FDataToPost + '&' + CurrBalise + '=' + CNT_FLOTTE3_IN_DEUTERIUM;

           end else begin
              // Input box inconnu...
              // On déclenche une erreur au lieu de la laisser à sa valeur par défaut
              FError := true;

           end;



        end else if SameText(CurrBalise, 'radio') then begin
           // Sélection de l'ordre
           // <input type="radio" name="order" value="3">Transport
           // <input type="radio" name="order" value="1">Attaquer

           if FDataToPost = '' then begin
              // Il y a obligatoirement déjà quelque chose dans FDataToPost
              FError := true;
              exit;
           end;

           CurrBalise := Attributes.Value('name');

           if SameText(CurrBalise, 'order') then begin

              if FRadioDejaTraiter = false then begin
                 FDataToPost := FDataToPost + '&' + CurrBalise + '=' + CNT_FLOTTE3_IN_ORDRE;
                 FRadioDejaTraiter := true;
              end;

              // Indique que cet ordre est disponible
              CurrBalise := trim(Attributes.Value('value'));

              if CurrBalise <> '' then
                 FListOrdreDispo.Add(CurrBalise);

           end else begin
              // radio inconnu
              // On déclenche une erreur
              FError := true;

           end;

        end;

    end;



end;

procedure TParsingFlotte3_OrdreEtRessources.ScanOnEndTag(Sender: TObject; TagName: string);
begin
    TagName := lowercase(TagName);

    if TagName = 'form' then begin
       FInForm := false;

    end;

end;

procedure TParsingFlotte3_OrdreEtRessources.ScanOnContent(Sender: TObject; Content: string);
begin
    // Pas de content à traiter
end;






end.
